<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <style>
        
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>
    <script src="js/jquery.min.js"></script>
    <script src="js/sessiontolls.js"></script>
    <script src="js/urltools.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>

        <span id="userElement" style="display:none;">
            <a href="myblog_list.html">我的博客</a>
            <a href="blog_add.html">写博客</a>
            <a href="javascript:logout()">注销</a>
        </span>

        <a id="loginElement" href="login.html">登陆</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div  class="container-right" style="width: 100%;">
           <div id="artlist">
                <!-- 每一篇博客包含标题, 摘要, 时间 -->
                <!-- <div class="blog">
                    <div class="title">我的第一篇博客</div>
                    <div class="date">2021-06-02</div>
                    <div class="desc">正文</div>
                    <a href="blog_content.html?id=1" class="detail">查看全文 &gt;&gt;</a>
                </div> -->
           </div>
            
           
            <hr>
            <div id="pagediv" class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="doFirst()" >首页</button> 
                <button class="blog-pagnation-item" onclick="doPre()">上一页</button>  
                <button class="blog-pagnation-item" onclick="doNext()">下一页</button>
                <button class="blog-pagnation-item" onclick="doLast()">末页</button>
            </div>
        </div>
    </div>
    <script>
        function islogin(){
                jQuery.ajax({
                    url:"/user/islogin",
                    type:"post",
                    data:{},
                    success:function(res){
                       if(res.code == 200 && res.data == 1){
                            jQuery("#loginElement").hide();
                            jQuery("#userElement").show();
                       }else{
                        jQuery("#loginElement").show();
                            jQuery("#userElement").hide();
                       } 
                    }
                });
            }
            islogin();
            var pageIndex = 1;//当前页码 默认从1开始
            var pageSize = 2;//每页条数
            var pageCount =1;//总数
            //初始化参数
            function pageInit(){
                var paramPageIndex = getParamByKey("pageIndex");
                if(paramPageIndex != null){
                    pageIndex = paramPageIndex;
                }
                var parampageSize = getParamByKey("pageSize");
                if(parampageSize != null){
                    pageSize = parampageSize;
                }
                doAjax();
            }
            pageInit();

            function doFirst(){
                if(pageIndex <= 1){
                    alert("已经在首页了!");
                    return false;
                }
                pageIndex = 1;
                 // doAjax();
                 location.href  = "blog_list.html?pageIndex="+pageIndex+"&pageSize="+pageSize;
            }
            function doPre(){
                if(pageIndex <= 1){
                    alert("已经在首页了!");
                    return false;      
                }
                --pageIndex;
                location.href = "blog_list.html?pageIndex="+pageIndex+"&pageSize"+pageSize;
            }
            function doNext(){
                if(pageIndex >=pageCount){
                    alert("已经在末页了!");
                    return false;
                }
                ++pageIndex;
                location.href = "blog_list.html?pageIndex="+pageIndex+"&pageSize"+pageSize;
            }
            function doLast(){
                if(pageIndex >=pageCount){
                    alert("已经在末页了!");
                    return false;
                }
                location.href = "blog_list.html?pageIndex="+pageCount+"&pageSize"+pageSize;

            }
            //请求后端 ，得到数据展示
            function doAjax(){
                 jQuery.ajax({
                    url:"/art/getlistbypage",
                    type:"post",
                    data:{
                        "pageIndex":pageIndex,
                        "pageSize":pageSize
                    },
                    success:function(res){
                        if(res.code == 200 && res.data.length > 0){
                            var artListHtml = "";
                            for(var i = 0;i<res.data.length;i++){
                                var itm = res.data[i];
                                artListHtml += '<div class="blog" >';
                                    artListHtml += ' <div class="title">'+itm.title+'</div>';
                                    artListHtml += '<div class="date">'+itm.createtime+'</div>'
                                    artListHtml += '<div class="desc">'+itm.content+'</div>';
                                    artListHtml += ' <a href="blog_content.html?id='+itm.id+'" class="detail">查看全文 &gt;&gt;</a>';
                                    artListHtml += ' </div>';


                            }
                            jQuery("#artlist").html(artListHtml);
                            jQuery("#pagediv").show();
                              
                        }else{
                            jQuery("#pagediv").hide();
                            jQuery("#artlist").html("<h2 style='magin-left:100px;margin-top:100px;'>暂无文章!</h2>");
                        }
                    }
                 });
                 //查询文章总数，得出总页数
                 jQuery.ajax({
                    url:"/art/getcount",
                    type:"post",
                    data:{},
                    success:function(res){
                        if(res.code ==200 && res.data > 0){
                                pageCount =Math.ceil(res.data/pageSize);//向上取整

                        }
                    }
                 });
            }
    </script>
</body>
</html>